package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.dto.RecordDTO;
import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.auth.dto.AuthUserDTO;
import com.neusoft.bizcore.auth.model.AuthUser;
import com.neusoft.bizcore.auth.repository.AuthUserRepository;
import com.neusoft.bizcore.auth.service.AuthUserService;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.fixed.converter.AssetLifeCycleConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetLifeCycleDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AssetLifeCycle;
import com.neusoft.neuiotms.dcam.fixed.repository.AssetLifeCycleRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AssetLifeCycleService;
import com.neusoft.neuiotms.dcam.workflow.dto.BaofeiKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.BaofeiSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.DiaoboKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.DiaoboSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.GenghuanKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.GuihuanSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.JieyongSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.LingyongKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.LingyongSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.PeifaSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.RukuSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.TuikuKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.TuikuSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.WeixiuKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.WeixiuSLDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.ZhuanyiKJDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.ZhuanyiSLDTO;
import com.neusoft.neuiotms.dcam.workflow.service.BaofeiKJService;
import com.neusoft.neuiotms.dcam.workflow.service.BaofeiSLService;
import com.neusoft.neuiotms.dcam.workflow.service.DiaoboKJService;
import com.neusoft.neuiotms.dcam.workflow.service.DiaoboSLService;
import com.neusoft.neuiotms.dcam.workflow.service.GenghuanKJService;
import com.neusoft.neuiotms.dcam.workflow.service.GuihuanSLService;
import com.neusoft.neuiotms.dcam.workflow.service.JieyongSLService;
import com.neusoft.neuiotms.dcam.workflow.service.LingyongKJService;
import com.neusoft.neuiotms.dcam.workflow.service.LingyongSLService;
import com.neusoft.neuiotms.dcam.workflow.service.PeifaSLService;
import com.neusoft.neuiotms.dcam.workflow.service.RukuKJService;
import com.neusoft.neuiotms.dcam.workflow.service.RukuSLService;
import com.neusoft.neuiotms.dcam.workflow.service.TuikuKJService;
import com.neusoft.neuiotms.dcam.workflow.service.TuikuSLService;
import com.neusoft.neuiotms.dcam.workflow.service.WeixiuKJService;
import com.neusoft.neuiotms.dcam.workflow.service.WeixiuSLService;
import com.neusoft.neuiotms.dcam.workflow.service.ZhuanyiKJService;
import com.neusoft.neuiotms.dcam.workflow.service.ZhuanyiSLService;

@Service
public class AssetLifeCycleServiceImpl implements AssetLifeCycleService {

    @Autowired
    private AssetLifeCycleConverter assetLifeCycleConverter;
    @Autowired
    private AssetLifeCycleRepository assetLifeCycleRepository;
    @Autowired
    private ProcessInstanceService processInstanceService;
    @Autowired
    private AuthUserRepository authUserRepository;
    @Autowired
    private AuthUserService authUserService;
    @Autowired
    private RukuKJService rukuKJService;
    @Autowired
    private TuikuKJService tuikuKJService;
    @Autowired
    private BaofeiKJService baofeiKJService;
    @Autowired
    private WeixiuKJService weixiuKJService;
    @Autowired
    private DiaoboKJService diaoboKJService;
    @Autowired
    private ZhuanyiKJService zhuanyiKJService;
    @Autowired
    private LingyongKJService lingyongKJService;
    @Autowired
    private BaofeiSLService baofeiSLService;
    @Autowired
    private TuikuSLService tuikuSLService;
    @Autowired
    private WeixiuSLService weixiuSLService;
    @Autowired
    private ZhuanyiSLService zhuanyiSLService;
    @Autowired
    private DiaoboSLService diaoboSLService;
    @Autowired
    private PeifaSLService peifaSLService;
    @Autowired
    private RukuSLService rukuSLService;
    @Autowired
    private LingyongSLService lingyongSLService;
    @Autowired
    private JieyongSLService jieyongSLService;
    @Autowired
    private GuihuanSLService guihuanSLService;
    @Autowired
    private GenghuanKJService genghuanKJService;

    @Override
    public List<AssetLifeCycleDTO> index(final Searchable searchable) {
        final List<AssetLifeCycle> models = this.assetLifeCycleRepository.search(searchable);
        return this.assetLifeCycleConverter.toListDTO(models);
    }

    @Override
    public Page<AssetLifeCycleDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<AssetLifeCycle> models = this.assetLifeCycleRepository.search(pageable, searchable);
        return this.assetLifeCycleConverter.toPageDTO(models);
    }

    @Override
    public AssetLifeCycleDTO create(final AssetLifeCycleDTO dto) {
        final AssetLifeCycle model = this.assetLifeCycleConverter.toModel(dto);
        this.assetLifeCycleRepository.save(model);

        return this.assetLifeCycleConverter.toDTO(model);
    }

    @Override
    public List<AssetLifeCycleDTO> batchCreate(final List<AssetLifeCycleDTO> dtos) {

        final List<AssetLifeCycle> models =
                dtos.stream().map(s -> this.assetLifeCycleConverter.toModel(s)).collect(Collectors.toList());
        this.assetLifeCycleRepository.saveAll(models);

        return this.assetLifeCycleConverter.toListDTO(models);
    }

    @Override
    public List<AssetLifeCycleDTO> detailByCode(final String code, final UserBean user) {
        final List<AssetLifeCycle> models = this.assetLifeCycleRepository.findByCodeOrderByCreateTimeDesc(code);
        final List<AssetLifeCycleDTO> listDTO = this.assetLifeCycleConverter.toListDTO(models);
        for (final AssetLifeCycleDTO assetLifeCycleDTO : listDTO) {
            final List<RecordDTO> result = this.processInstanceService.getRecords(assetLifeCycleDTO.getWorkNum());
            for (final RecordDTO recordDTO : result) {
                switch (recordDTO.getNodeName()) {
                case "物资入库申请":
                    final RukuKJDTO rukuKJDTO =
                            this.rukuKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(rukuKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(rukuKJDTO.getCreateUser()));
                    break;
                case "物资退库申请":
                    final TuikuKJDTO tuikuKJDTO =
                            this.tuikuKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(tuikuKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(tuikuKJDTO.getCreateUser()));
                    break;
                case "物资报废申请":
                    final BaofeiKJDTO baofeiKJDTO =
                            this.baofeiKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(baofeiKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(baofeiKJDTO.getCreateUser()));
                    break;
                case "物资维修申请":
                    final WeixiuKJDTO weixiuKJDTO =
                            this.weixiuKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(weixiuKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(weixiuKJDTO.getCreateUser()));
                    break;
                case "物资调拨申请":
                    final DiaoboKJDTO diaoboKJDTO =
                            this.diaoboKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(diaoboKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(diaoboKJDTO.getCreateUser()));
                    break;
                case "物资转移申请":
                    final ZhuanyiKJDTO zhuanyiKJDTO =
                            this.zhuanyiKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(zhuanyiKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(zhuanyiKJDTO.getCreateUser()));
                    break;
                case "物资领用申请":
                    final LingyongKJDTO lingyongKJDTO =
                            this.lingyongKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(lingyongKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(lingyongKJDTO.getCreateUser()));
                    break;
                case "资产处置申请":
                    final BaofeiSLDTO baofeiSLDTO =
                            this.baofeiSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(baofeiSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(baofeiSLDTO.getCreateUser()));
                    break;
                case "资产退库申请":
                    final TuikuSLDTO tuikuSLDTO =
                            this.tuikuSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(tuikuSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(tuikuSLDTO.getCreateUser()));
                    break;
                case "资产维修申请":
                    final WeixiuSLDTO weixiuSLDTO =
                            this.weixiuSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(weixiuSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(weixiuSLDTO.getCreateUser()));
                    break;
                case "资产转移申请":
                    final ZhuanyiSLDTO zhuanyiSLDTO =
                            this.zhuanyiSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(zhuanyiSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(zhuanyiSLDTO.getCreateUser()));
                    break;
                case "资产调拨申请":
                    final DiaoboSLDTO diaoboSLDTO =
                            this.diaoboSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(diaoboSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(diaoboSLDTO.getCreateUser()));
                    break;
                case "资产配发清单":
                    final PeifaSLDTO peifaSLDTO =
                            this.peifaSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(peifaSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(peifaSLDTO.getCreateUser()));
                    break;
                case "资产入库申请":
                    final RukuSLDTO rukuSLDTO =
                            this.rukuSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(rukuSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(rukuSLDTO.getCreateUser()));
                    break;
                case "资产领用申请":
                    final LingyongSLDTO lingyongSLDTO =
                            this.lingyongSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(lingyongSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(lingyongSLDTO.getCreateUser()));
                    break;
                case "资产借用申请":
                    final JieyongSLDTO jieyongSLDTO =
                            this.jieyongSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(jieyongSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(jieyongSLDTO.getCreateUser()));
                    break;
                case "资产归还申请":
                    final GuihuanSLDTO guihuanSLDTO =
                            this.guihuanSLService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(guihuanSLDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(guihuanSLDTO.getCreateUser()));
                    break;
                case "更换申请":
                    final GenghuanKJDTO genghuanKJDTO =
                            this.genghuanKJService.detail(assetLifeCycleDTO.getWorkNum(), recordDTO.getTaskId(), user);
                    assetLifeCycleDTO.setApproveName(genghuanKJDTO.getCreateUserName());
                    assetLifeCycleDTO.setApproveDepartment(this.getDepartment(genghuanKJDTO.getCreateUser()));
                    break;
                default:
                    break;
                }
            }
        }
        return listDTO;
    }

    private List<String> getDepartment(final String userName) {
        //    private String getDepartment(final String userName) {
        //        final StringBuffer str = new StringBuffer();
        final AuthUser authUserModel = this.authUserRepository.findByUsername(userName);
        final AuthUserDTO authUserDTO = this.authUserService.details(authUserModel.getId());
        final List<String> orgs = new ArrayList<>();
        for (int i = 0; i < authUserDTO.getOganizations().size(); i++) {
            orgs.add(authUserDTO.getOganizations().get(i).getName());
        }

        return orgs;
    }

}
